﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.Entity;
using ClassLibrary.DAO;

public partial class Admin_Login : System.Web.UI.Page
{
    TblCustomersDAO userDAO = new TblCustomersDAO(Global.CONNECTION_STRING);
    HttpCookie cookie_username = null;
    HttpCookie cookie_pass = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if ( Request.Cookies["username"] != null)
            {
                txtEmail.Text = Request.Cookies["username"].Value;
                
            }
        }
    }
    protected void btLogin_Click(object sender, EventArgs e)
    {
        bool bFlag = false;
        try
        {
            // khai báo biến cookie
            cookie_username = new HttpCookie("username", txtEmail.Text);
            

            // Gan ngay song cho cookie
            cookie_username.Expires = DateTime.Now.AddDays(30);
            

            //Them cookie

            if (cbRemember.Checked)
            {
                cookie_username.Value = txtEmail.Text;
               
                Response.Cookies.Add(cookie_username);
                
            }
            else
            {
                cookie_username.Value = null;
               
            }
            if (txtEmail.Text != "")
            {

                TblCustomers userAccount = userDAO.GetAdmin(txtEmail.Text.Trim());
               
                 if (userAccount != null && userAccount.CusID != 0)
                {
                    Session[Global.SESSION_ADMIN_USER] = userAccount;
                    bFlag = true;
                   
                }
                else
                    {
                        lbMessenge.Text = "Username (password) not coincide .";
                        lbMessenge.Visible = true;
                    }

            }
            else
            {
                lbMessenge.Text = "Not Found Email.";
                lbMessenge.Visible = true;
            }
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
        if (bFlag)
            Response.Redirect("~/Admin/Default.aspx");
    }
}
